<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    {% load static %}
    <link rel="stylesheet" href="{% static '/css/bootstrap.min.css' %}">
    <script src="{% static '/js/jquery-3.2.1.min.js' %}"></script>
    <script src="{% static '/js/bootstrap.min.js' %}"></script>
    <script>
        function checkname() {
            let name = $("#name").val();
            let sendData = {
                "name":name,
                "csrfmiddlewaretoken":"{{ csrf_token }}"
            };
            if (!name){
                return false;
            }else {
                $.ajax({
                    url:"/checkDishesName/",
                    data:sendData,
                    type:"post",
                    success:function (data) {
                        let result = data["resule"];
                        if (result === "可用"){
                            $("#checkname").attr("class", "input-group col-sm-8 col-md-offset-2 has-success");
                            return true;
                        }else {
                            $("#checkname").attr("class", "input-group col-sm-8 col-md-offset-2 has-error");
                            return false;
                        }
                    }
                })
            }
        }
        function checkinput(theId) {
            let flag1 = $("#check"+theId).is(':checked');
            let flag2 = !!$("#ingrnum"+theId).val();
            if (!((flag1 && flag2) || (!flag1 && !flag2))) {
                $("#tr" + theId).attr("class", "has-error");
                return false;
            } else {
                $("#tr" + theId).attr("class", "has-success");
                return true;
            }
        }
        function checkall() {
            return $(".has-error").length ==0;
        }
    </script>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            <h3>这里是添加新菜品的页面</h3>
            <a href="/kitchenHome/">返回后厨主页</a>
        </div>
    </div>
</div>
<div class="container">
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            <form method="post" class="form-horizontal" action="" onsubmit="return checkall()">
                {% csrf_token %}
                <table class="table-bordered table-condensed">
                    <tr>
                        <td>食材名称</td>
                        <td>食材数量</td>
                    </tr>
                    {% for ing in ingrlist %}
                        <tr id="tr{{ ing.id }}" class="">
                            <td>
                                <div class="checkbox">
                                    <label>
                                        <input onblur="checkinput('{{ ing.id }}')" id="check{{ ing.id }}" type="checkbox" name="ingr" value="{{ ing.id }}">{{ ing.name }}
                                    </label>
                                </div>
                            </td>
                            <td>
                                <div class="form-group">
                                    <div class="input-group col-sm-8 col-md-offset-2">
                                        <span class="input-group-addon"><label for="pwd1">数量</label></span>
                                        <input onblur="checkinput('{{ ing.id }}')" id="ingrnum{{ ing.id }}" class="form-control" type="number" name="ingrnum{{ ing.id }}">
                                    </div>
                                </div>
                            </td>
                        </tr>
                    {% empty %}
                        <span>暂时没有食材种类，请添加</span>
                    {% endfor %}
                </table>
                <div class="form-group">
                    <div class="input-group col-sm-8 col-md-offset-2" id="checkname">
                        <span class="input-group-addon"><label for="pwd1">菜品名称</label></span>
                        <input class="form-control" required="required" type="text" id="name" name="name" onblur="checkname()"><br>
                    </div>
                </div>
                <div class="form-group">
                    <div class="input-group col-sm-8 col-md-offset-2">
                        <span class="input-group-addon"><label for="pwd1">菜品价格</label></span>
                        <input class="form-control" required="required" type="text" name="price"><br>
                    </div>
                </div>
                <div class="form-group">
                    <div class="input-group col-sm-offset-3 col-sm-6">
                        <button class="btn btn-primary btn-block">添加菜品</button><span>{{ msg }}</span>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
</html>